" Use pandoc to convert current file to html and open {{{1
if !empty($ENV_MAC)
  function! PandocToHtmlFunction()
    " generate the html file using pandoc
    !pandoc -o $TEMP_FILE_DIR/temp.html "%"
    " open the above-generated html file
    !open $TEMP_FILE_DIR/temp.html
    " the above somehow messes up the display, redraw
    redraw!
  endfunction
else
  function! PandocToHtmlFunction()
    " generate the html file using pandoc
    !pandoc -o $TEMP_FILE_DIR/temp.html "%"
    " open the above-generated html file
    !open $TEMP_FILE_DIR/temp.html
    " the above somehow messes up the display, redraw
    redraw!
  endfunction
endif

command! PandocToHtml silent! call PandocToHtmlFunction()

" Use pandoc to convert current file to pdf and open {{{1
if !empty($ENV_MAC)
  function! PandocToPdfFunction()
    " generate the pdf file using pandoc
    !pandoc --toc --toc-depth=2 -o $TEMP_FILE_DIR/temp.pdf "%"
    " open the output html
    !open $TEMP_FILE_DIR/temp.pdf
    " the above somehow messes up the display, redraw
    redraw!
  endfunction
else
  function! PandocToPdfFunction()
    " generate the pdf file using pandoc
    !pandoc --toc --toc-depth=2 -o $TEMP_FILE_DIR/temp.pdf "%"
    " open the output html
    !open $TEMP_FILE_DIR/temp.pdf
    " the above somehow messes up the display, redraw
    redraw!
  endfunction
endif

command! PandocToPdf silent! call PandocToPdfFunction()

" Use pandoc to auto-format {{{1
" Pros:
" - Idiot-proof formatting
" - Auto fixing special characters by prepending slash \
" Cons:
" - Sometimes it indent a big paragraph wrongly
" - Annoying # replacing === and ## replacing ---
let g:markdown_format_id = 0
function! TogglePandocMarkdownReformatFunction()
  if (g:markdown_format_id == 0)
    let &formatprg="pandoc --from=markdown --to=html | pandoc --from=html --to=markdown"
    let g:markdown_format_id = 1
    echo 'markdown reformat using pandoc'
  elseif (g:markdown_format_id == 1)
    let &formatprg=""
    let g:markdown_format_id = 0
    echo 'default reformat'
  endif
endfunction

command! TogglePandocMarkdownReformat silent! call TogglePandocMarkdownReformatFunction()
